package dk.dashboardFP.panels.introspectionPanel {
	import dk.dashboardFP.core.DashboardFPSettings;
	import dk.dashboardFP.panels.settingspanel.SettingsPanel;
	import dk.dashboardFP.core.DashboardFPCore;
	import dk.dashboardFP.panels.subPanels.ui.windowSubPanelUI.WindowSubPanelUI;
	import dk.dashboardFP.panels.tempPanel.AbstractTempWindowPanel;

	/**
	 * @author josef
	 */
	public class IntrospectionPanel extends AbstractTempWindowPanel {
		private var _doWC : IntrospectionWindowContent;
		private var _rootNodeView : IntrospectionNodeView;
		private var _oWindow : IntrospectionSubPanel;

		public function IntrospectionPanel(doDashboardFPCore : DashboardFPCore, sPanelName : String) {
			super(doDashboardFPCore, sPanelName);
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// setup
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		override protected function createPanelElements() : void {
			_oWindow = new IntrospectionSubPanel(this, "Introspection window");
			addSubPanel(_oWindow);
			
			setupElements();
		}
		
		
		private function setupElements() : void {
			
			//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
			// setup windowcontent
			//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
			
			_doWC = new IntrospectionWindowContent();
			WindowSubPanelUI(_oWindow.doSubPanelUI).addWindowContent(_doWC);
			
			//:::::::
			// search 
			_doPanelUI.addEventListener(InputElementDFPEvent.VALUE_CHANGE, onSearchChange);
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// change
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function onSearchChange(event : InputElementDFPEvent) : void {
			var searchField:InputElementDFP = event.target as InputElementDFP;
			_doWC.search(searchField.value);
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// introspcet
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		public function introspectObject(oIntroSpectionNode : IntrospectionNode, preSearch:String="") : void {
			
			//:::::::
			// node 
			_rootNodeView = getRootNode(oIntroSpectionNode);
			_doWC.renderIntrospectionTree(_rootNodeView);
			
			if (DashboardFPSettings.get_oInstance().numAutoOpenLevels.nValue>0) {
				_rootNodeView.openNode(DashboardFPSettings.get_oInstance().numAutoOpenLevels.nValue);
			}
			
			//:::::::
			// presearch 
			if (preSearch!="") {
				_doWC.search(preSearch);
				IntrospectionSubPanelUI(_oWindow.doSubPanelUI).searchFieldElement.value = preSearch;
				IntrospectionSubPanelUI(_oWindow.doSubPanelUI).searchFieldElement.selectAll();
			} else {
				IntrospectionSubPanelUI(_oWindow.doSubPanelUI).searchFieldElement.value="";
			}
			
			//:::::::
			// set search focus 
			IntrospectionSubPanelUI(_oWindow.doSubPanelUI).searchFieldElement.setInFocus();
		}

		private function getRootNode(oIntroSpectionNode : IntrospectionNode) : IntrospectionNodeView {
			
			if (oIntroSpectionNode is DCIntrospectionNode) {
				return new DCIntrospectionNodeView(oIntroSpectionNode);
			} 
			
			return new IntrospectionNodeView(oIntroSpectionNode);
		}
	}
}
